//
//  UIView+CornerRadius.m
//  MagilitSleep
//
//  Created by ljh on 17/1/10.
//  Copyright © 2017年 ljh. All rights reserved.
//

#import "UIView+CornerRadius.h"

@implementation UIView (CornerRadius)
- (void)cornerSideType:(ViewSideType)sideType withCornerRadius:(CGFloat)cornerRadius
{
    
    CGSize cornerSize = CGSizeMake(cornerRadius, cornerRadius);
    UIBezierPath *maskPath;
    
    switch (sideType) {
        case kViewSideTypeTop:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
                                                   cornerRadii:cornerSize];
        }
            break;
        case kViewSideTypeLeft:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerBottomLeft)
                                                   cornerRadii:cornerSize];
        }
            break;
        case kViewSideTypeBottom:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight)
                                                   cornerRadii:cornerSize];
        }
            break;
        case kViewSideTypeRight:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:(UIRectCornerTopRight|UIRectCornerBottomRight)
                                                   cornerRadii:cornerSize];
        }
            break;
        default:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:UIRectCornerAllCorners
                                                   cornerRadii:cornerSize];
        }
            break;
    }
    
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    
    self.layer.mask = maskLayer;
    
//    [self.layer setMasksToBounds:YES];
}

- (void)cornerRectType:(UIRectCorner)type withCornerRadius:(CGFloat)cornerRadius
{
    
    CGSize cornerSize = CGSizeMake(cornerRadius, cornerRadius);
    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:type
                                                   cornerRadii:cornerSize];
    
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    
    self.layer.mask = maskLayer;
    
}



- (void)cornerRectType:(UIRectCorner)type withCornerRadius:(CGFloat)cornerRadius lineColor:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth
{
    
    CGSize cornerSize = CGSizeMake(cornerRadius, cornerRadius);
    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                     byRoundingCorners:type
                                           cornerRadii:cornerSize];
    
    maskPath.lineWidth = 5;
//    maskPath.
//    UIColor *strokeColor = lineColor;
//    [strokeColor set];
//    [maskPath stroke];//设置线条颜色
    
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    maskLayer.strokeColor = lineColor.CGColor;
    maskLayer.lineWidth = 5;

//    self.layer.mask = maskLayer;
    [self.layer addSublayer:maskLayer];
    
}

/**
 设置渐变色 左到右 fd56b4 -> ff3657
 */
-(void)setGradientColor
{
//    UIColor *fromColor = UICor;
    [self setGradientColor:[UIColor colorWithHexString:@"fd56b4"] endColor:[UIColor blackColor]];
}

/**
 设置渐变色  左到右
 
 @param fromColor 最左边颜色
 @param endColor 最右边颜色
 */
-(void)setGradientColor:(UIColor *)fromColor endColor:(UIColor *)endColor
{
    
    //绘制渐变色颜色的方法
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    //  创建渐变色数组，需要转换为CGColor颜色
    gradientLayer.colors = @[(__bridge id)fromColor.CGColor,(__bridge id)endColor.CGColor];
    //  设置渐变颜色方向，左上点为(0,0), 右下点为(1,1)
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    //  设置颜色变化点，取值范围 0.0~1.0
    gradientLayer.locations = @[@0,@1];
    [self.layer insertSublayer:gradientLayer atIndex:0];
}

-(void)setGradientColor:(UIColor *)fromColor endColor:(UIColor *)endColor direction:(GradientColorDirection)direction
{
    CGPoint startPoint = CGPointMake(0, 0);
    CGPoint endPoint   = CGPointMake(1, 1);
    if (direction == GradientColorDirection_Horizontal) {
        startPoint = CGPointMake(0, 0.5);
        endPoint   = CGPointMake(1, 0.5);
    } else if (direction == GradientColorDirection_Vertical) {
        startPoint = CGPointMake(0.5, 0);
        endPoint   = CGPointMake(0.5, 1);
    }
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    //  创建渐变色数组，需要转换为CGColor颜色
    gradientLayer.colors = @[(__bridge id)fromColor.CGColor,(__bridge id)endColor.CGColor];
    //  设置渐变颜色方向，左上点为(0,0), 右下点为(1,1)
    gradientLayer.startPoint = startPoint;
    gradientLayer.endPoint   = endPoint;
    //  设置颜色变化点，取值范围 0.0~1.0
    gradientLayer.locations = @[@0,@1];
    [self.layer insertSublayer:gradientLayer atIndex:0];
    
}


@end
